package car;

import operation.DBHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Cars {
    private List<Vehicle> carList = new ArrayList<>();
    private List<Vehicle> busList = new ArrayList<>();
    private List<Vehicle> trunkList = new ArrayList<>();

    public Cars() {
        //初始化车辆
        initializeVehicles();
        //初始化数据库
        insertVehiclesIntoDatabase();
    }

    private void initializeVehicles() {
        Car car1 = new Car("京NY28588", "宝马", "-X6", 800);
        Car car2 = new Car("京CNY3284", "宝马", "-550i", 600);
        Car car3 = new Car("京NT37465", "别克", "林荫大道", 300);
        Car car4 = new Car("京NT96968", "别克", "GL8", 600);
        carList.add(car1);
        carList.add(car2);
        carList.add(car3);
        carList.add(car4);
        Bus bus1 = new Bus("京6566754", "金杯", "16座", 800);
        Bus bus2 = new Bus("京8696997", "金杯", "16座", 800);
        Bus bus3 = new Bus("京9696996", "金龙", "34座", 1500);
        Bus bus4 = new Bus("京8696998", "金龙", "34座", 1500);
        busList.add(bus1);
        busList.add(bus2);
        busList.add(bus3);
        busList.add(bus4);
        Trunk trunk1 = new Trunk("京NB11111", "一汽", "10", 2000);
        Trunk trunk2 = new Trunk("京VB22222", "一汽", "20", 2500);
        Trunk trunk3 = new Trunk("京NM33333", "雷诺", "10", 3000);
        Trunk trunk4 = new Trunk("京KL44444", "雷诺", "20", 3500);
        trunkList.add(trunk1);
        trunkList.add(trunk2);
        trunkList.add(trunk3);
        trunkList.add(trunk4);
    }

    private void insertVehiclesIntoDatabase() {
        // 将汽车信息插入数据库
        for (Vehicle car : carList) {
            DBHelper.insertVehicle(car);
        }

        // 将公交车信息插入数据库
        for (Vehicle bus : busList) {
            DBHelper.insertVehicle(bus);
        }

        // 将卡车信息插入数据库
        for (Vehicle trunk : trunkList) {
            DBHelper.insertVehicle(trunk);
        }
    }

    public Vehicle getVehicle(int type, int pos) {
        if (type == 1) {
            return carList.get(pos);
        } else if (type == 2) {
            return busList.get(pos);
        } else if (type == 3) {
            return trunkList.get(pos);
        } else {
            System.out.println("输入错误!!!");
            return null;
        }
    }

    public List<Vehicle> getList(int type) {
        if (type == 1) {
            return carList;
        } else if (type == 2) {
            return busList;
        } else if (type == 3) {
            return trunkList;
        } else {
            return null;
        }
    }


}
